<template>
  <view class="normal-login-container">
	  <!-- <view class="top">
	    <view class="top-return" @click="returnclick()"></view>
	    <view class="top-title">登录</view>
	  </view>
    <view class="header">
      
    </view>
    <view class="login-form-content shadow">
	  <view class="switch">
		  <view class="switch-piece" :class="{ 'isselect': select }" @click="selectit(true)">
		  	短信登录
		  </view>
		  <view class="switch-piece" :class="{ 'isselect': !select }" @click="selectit(false)">
		  	密码登录
		  </view>
	  </view>
      <view class="input-item flex align-center">
        <view class="icon">
			<view class="iconuser"></view>
		</view>
        <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
      </view>
      <view class="input-item flex align-center">
        <view class="icon">
			<view class="iconpassword"></view>
		</view>
        <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
      </view>
      <view class="input-item flex align-center" v-if="captchaEnabled">
        <view class="iconfont icon-code icon"></view>
        <input v-model="loginForm.code" type="number" class="input" placeholder="请输入验证码" maxlength="4" />
        <image :src="codeUrl" @click="getCode" class="login-code-img"></image>
      </view>
      <view class="action-btn">
        <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
      </view>
    </view> -->
  </view>
</template>

<script>
  import { getCodeImg,loginByZXM,getInfo } from '@/api/login'
  import { setToken, getToken, removeToken } from "@/utils/auth";
  import { getinfo, setinfo, removeinfo } from '@/utils/auth5'
  export default {
    data() {
      return {
        ishow: false,
        cubeUserId:'6641c4b2677d391706191c1d'
        // codeUrl: "",
        // captchaEnabled: true,
        // globalConfig: getApp().globalData.config,
        // loginForm: {
        //   username: "admin",
        //   password: "admin123",
        //   code: "",
        //   uuid: '',
		    //   isApp: "1"
        // },
		    // select: false,
      }
  },
  onShow() {
    console.log("getToken()", getToken());
    if (this.ishow) {
      this.getdata();
    } else {
      // this.cubeUserId = "637caa9ea88122ce644eb95b";
      // this.login();
      this.Initialize()
    }
  },
	beforeCreate(){
		//没有孩子信息，跳转到这里办学在杭州添加孩子页面
		//  window.location.href="https://mapi.zjzwfw.gov.cn/web/mgop/gov-open/zj/2002238519/reserved/index.html?redirect=addchild";
		// let arr = this.$store.state.user.baseUrl.split(':')
		// let burl = arr[0]+':'+arr[1]
		// let url = 'https://www.mhjyy.cn/oauth/oauthserver/index?client_id=F8ADCDC81DD8BE0773DA93D97993945C&codeType=1&redirect_uri=https://wendao.wasu.cn/h5/index.html#'
		// window.location.href = url
	},
  created() {
		// 临时
		// this.Initialize()
	},
  methods: {
    Initialize() {
       ZWJSBridge.onReady(() => {
         ZWJSBridge.openLink({
            // 浙里办APP
            url: "https://mapi.zjzwfw.gov.cn/web/mgop/gov-open/zj/2002202546/reserved/index.html?appId=137582&areaCode=330100&isIframe=true&redirectUrl=%2FchildrenList",
          });
          this.ishow = true;
      });
     
    },
    async getdata() {
      let child = await ZWJSBridge.getLocalStorage({ key: "child" });
      console.log('child',child);
      if(child && child.child){
        this.cubeUserId = JSON.parse(child.child).cubeUserId;
        // this.$store.state.global.personal = child.child;
        ZWJSBridge.removeLocalStorage({ key:'child' })
        this.login();
      }else{
        uni.showToast({
          icon: "none",
          duration: 3000,
          title: "没有获取到学生信息",
        });
        setTimeout(() => {
          uni.reLaunch({
            url:'/pages/index'
          })
        }, 3000);
        //没有获取到孩子信息，用户在浙学码点击了返回（没有选择孩子）
      }
      
    },
    login() {
      loginByZXM({ cubeId: this.cubeUserId })
        .then((res) => {
          let token = res.token;
          setToken(token);
          console.log("getToken()", getToken());
          this.userInfo();
        })
        .catch((err) => {
          uni.showToast({
					  icon: "none",
					  duration: 3000,
					  title: "请求失败，学生信息不属于杭州地区或学龄段不符",
          });
					setTimeout(() => {
					  uni.reLaunch({
						  url:'/pages/index'
					  })
					}, 3000);
        });
    },
    userInfo() {
      this.$store.dispatch('GetInfo').then(res => {
         uni.showToast({
					  icon: "none",
					  duration: 3000,
					  title: "登陆成功",
					});
        setTimeout(() => {
          this.$tab.reLaunch('/pages/index')
        }, 3000);
      })
      // let that = this;
      // console.log("userInfo getToken()", getToken());
      // getInfo()
      //   .then((res) => {
      //     console.log("res", res);
      //     let info = res.user;
      //     setinfo(info);
      //     uni.showToast({
			// 		  icon: "none",
			// 		  duration: 3000,
			// 		  title: "登陆成功",
			// 		});
      //     setTimeout(() => {
			// 		  uni.reLaunch({
			// 			  url:'/pages/index'
			// 		  })
			// 		}, 3000);
      //   })
      //   .catch((error) => {});
    },
		// Initialize(){
		// 	this.getCode()
		// 	let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
		// 	if(flag){
		// 		this.loginForm.isApp=0
		// 	}else{
		// 		this.loginForm.isApp=1
		// 	}
		// },
		// selectit(e){
		// 	this.select = e
		// },
		// returnclick(){
		// 	this.$router.push('/pages/index')
		// },
    //   // 隐私协议
    //   handlePrivacy() {
    //     let site = this.globalConfig.appInfo.agreements[0]
    //     this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
    //   },
    //   // 用户协议
    //   handleUserAgrement() {
    //     let site = this.globalConfig.appInfo.agreements[1]
    //     this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
    //   },
    //   // 获取图形验证码
    //   getCode() {
    //     getCodeImg().then(res => {
    //       this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
    //       if (this.captchaEnabled) {
    //         this.codeUrl = 'data:image/gif;base64,' + res.img
    //         this.loginForm.uuid = res.uuid
    //       }
    //     })
    //   },
    //   // 登录方法
    //   async handleLogin() {
    //     if (this.loginForm.username === "") {
    //       this.$modal.msgError("请输入您的账号")
    //     } else if (this.loginForm.password === "") {
    //       this.$modal.msgError("请输入您的密码")
    //     } else if (this.loginForm.code === "" && this.captchaEnabled) {
    //       this.$modal.msgError("请输入验证码")
    //     } else {
    //       this.$modal.loading("登录中，请耐心等待...")
    //       this.pwdLogin()
    //     }
    //   },
    //   // 密码登录
    //   async pwdLogin() {
    //     this.$store.dispatch('Login', this.loginForm).then(() => {
    //       this.$modal.closeLoading()
    //       this.loginSuccess()
    //     }).catch(() => {
    //       if (this.captchaEnabled) {
    //         // this.getCode()
    //       }
    //     })
    //   },
      // 登录成功后，处理函数
      // loginSuccess(result) {
      //   // 设置用户信息
      //   this.$store.dispatch('GetInfo').then(res => {
      //     this.$tab.reLaunch('/pages/index')
      //   })
      // }
    }
  }
</script>

<style lang="scss">
page {
  background-color: #ffffff;
}

.normal-login-container {
  width: 100%;
  display: grid;
  justify-items: center;
  align-items: start;
  grid-template-rows: auto auto;
  display: none;

  .shadow {
    box-shadow: 0px 0px 5vw rgba(0, 0, 0, 0.08);
  }

  .top {
    width: 100%;
    height: 12.5vw;
    background: #ffffff;
    border: 1px solid #F6F7F8;
    display: grid;
    justify-items: center;
    align-items: center;
    /* grid-template-columns: 1fr; */
    /* grid-template-rows: 1fr 1fr; */
  }

  .top-title {
    font-size: 5vw;
  }

  .top-return {
    position: absolute;
    width: 5vw;
    height: 5vw;
    left: 5vw;
    top: 3.5vw;
    background: url(../static/index/return.svg);
    background-size: 100% 100%;
    cursor: pointer;
  }

  .header {
    width: 100vw;
    height: 68vw;
    background: url(../static/index/login4.png);
    background-size: 100% 100%;
  }

  .switch {
    width: 100%;
    display: grid;
    justify-items: center;
    align-items: center;
    grid-template-columns: 1fr 1fr;
  }

  .switch-piece {
    cursor: pointer;
  }

  .isselect {
    color: #1492FF;
    border-bottom: 3px solid #1492FF;
  }

  .iconuser {
    width: 21px;
    height: 24px;
    background: url(../static/index/user.png);
    background-size: 100% 100%;
  }

  .iconpassword {
    width: 21px;
    height: 24px;
    background: url(../static/index/password.png);
    background-size: 100% 100%;
  }

  .login-form-content {
    margin: -15vw auto 0 auto;
    width: 80vw;
    height: auto;
    padding: 10vw 0 20vw 0;
    border-radius: 1vw;
    background: #ffffff;

    .input-item {
      margin: 5vw;
      background-color: #f5f6f7;
      height: 45px;
      border-radius: 20px;

      .icon {
        font-size: 38rpx;
        margin-left: 10px;
        margin-right: 5px;
        color: #999;
      }

      .input {
        width: 100%;
        font-size: 14px;
        line-height: 20px;
        text-align: left;
        padding-left: 15px;
      }

    }

    .login-btn {
      margin: 40px 10vw;
      height: 45px;
    }
  }

  .easyinput {
    width: 100%;
  }
}

.login-code-img {
  height: 45px;
}</style>
